<?php

namespace app\service;

use think\Db;
use think\facade\Hook;
use app\service\ResourcesService;
use app\service\BrandService;
use app\service\RegionService;

/**
 * 作者服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class AuthorService {

    /**
     * 获取商品列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   array           $params [输入参数: where, field, is_photo]
     */
    public static function AuthorList($params = [], $flat = 0) {

        $where = empty($params['where']) ? [] : $params['where'];

        $order_by = empty($params['order_by']) ? 'id desc' : trim($params['order_by']);

        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;
        $data = Db::name('Author')->where($where)->order($order_by)->limit($m, $n)->select();

        return DataReturn('处理成功', 0, $data);
    }

    /**
     * 获取书单总数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-07
     * @desc    description
     * @param   [array]           $where [条件]
     */
    public static function authorTotal($where = []) {
        return (int) Db::name('Author')->where($where)->count();
    }

    /**
     * 商品保存
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-12-10T01:02:11+0800
     * @param    [array]          $params [输入参数]
     */
    public static function AuthorSave($params = []) {

        // 请求参数
        $p = [
            [
                'checked_type' => 'length',
                'key_name' => 'name',
                'checked_data' => '2,60',
                'error_msg' => '作者名称格式 2~60 个字符',
            ],
            [
                'checked_type' => 'length',
                'key_name' => 'introduce',
                'checked_data' => '160',
                'is_checked' => 1,
                'error_msg' => '作者介绍 最多160个字符',
            ]
        ];
        $ret = ParamsChecked($params, $p);
        if ($ret !== true) {
            return DataReturn($ret, -1);
        }
        // 其它附件
        $data_fields = ['head_img'];
        $attachment = ResourcesService::AttachmentParams($params, $data_fields);

        if ($attachment['code'] != 0) {
            return $attachment;
        } 
        // 基础数据
        $data = [
            'name' => $params['name'],
            'introduce' => $params['introduce'],
            'head_img' => $attachment['data']['head_img'],
        ];
        // 添加/编辑
        if (empty($params['id'])) {
            $data['add_time'] = time();
            $goods_id = Db::name('Author')->insertGetId($data);
        } else {

            if (Db::name('Author')->where(['id' => intval($params['id'])])->update($data)) {
                $goods_id = $params['id'];
            }
        }

        return DataReturn('操作成功', 0);
    }

    public static function getAuthorId($authorName) {
        $data = Db::name('Author')->where(['name' => $authorName])->value('id');

        if (empty($data)) {
            return DataReturn('处理失败', -1);
        }
        return DataReturn('处理成功', 0, $data);
    }

    /**
     * 作者删除

     * @version  1.0.0
     * @datetime 2018-12-07T00:24:14+0800
     * @param    [array]          $params [输入参数]
     */
    public static function authorDelete($params = []) {
        // 参数是否有误
        if (empty($params['id'])) {
            return DataReturn('作者id有误', -1);
        }


        // 删除商品
        if (Db::name('Author')->delete(intval($params['id']))) {

            return DataReturn('删除成功', 0);
        }

        return DataReturn('删除失败', -100);
    }

}

?>